package EGS.impl;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.m_resultData;
import WORKPLACE.stationClient;

public class EGS_execute_service555Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 555 上传测量数据 
	 */
	public EGS_execute_service555Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("555")) {// 上传测量数据 
				iSN = Mroot.element("body").element("panel").attribute("pcbID").getValue();
				iLayer = Mroot.element("body").element("panel").attribute("productSide").getValue();
				Element eleCheckList = Mroot.element("body").element("checkList");
				List<?> checkListSons = eleCheckList.elements();
				Mroot.element("body").remove(Mroot.element("body").element("panel"));
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				int listI=0;
				try {
					if (checkListSons.size() > 0) {
						int listLen = checkListSons.size();
						m_resultData[] iData = new m_resultData[listLen];
						for (listI = 0; listI < listLen; listI++) {
							if(listI==422) {
								System.out.print("testCounter");
							}
							iData[listI] = new m_resultData();
							iData[listI].pos=((Element) (checkListSons.get(listI))).attribute("pos").getValue();
							iData[listI].name = ((Element) (checkListSons.get(listI))).attribute("name").getValue();
							iData[listI].unit = ((Element) (checkListSons.get(listI))).attribute("unit").getValue();
							iData[listI].max = ((Element) (checkListSons.get(listI))).attribute("upperLimit")
									.getValue();
							iData[listI].min = ((Element) (checkListSons.get(listI))).attribute("lowerLimit")
									.getValue();
							iData[listI].value = ((Element) (checkListSons.get(listI))).attribute("value").getValue();
							iData[listI].result = ((Element) (checkListSons.get(listI))).attribute("state").getValue();
							try {
								iData[listI].equipment = ((Element) (checkListSons.get(listI))).attribute("equipment").getValue();
							}catch(Exception e) {
								iData[listI].equipment = "";
							}
						}
						iClient.uploadData(iSN, iData);
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(5551, "" + iSN + "_" + iStation + "_" + iLayer);
					}else {
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "测量项目0条");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(5551, "" + iSN + "_" + iStation + "_" + iLayer+"_"+"测量项目0条");
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5550, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
